﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using thinger.HeatingControlProject._2.数据层;

namespace thinger.HeatingControlProject._1.界面层
{
    public partial class FrmHistoryQuery : Form
    {
        #region 无边框拖动 

        private Point mPoint;
        private void Panel_MouseDown(object sender, MouseEventArgs e)
        {
            mPoint = new Point(e.X, e.Y);
        }

        private void Panel_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                this.Location = new Point(this.Location.X + e.X - mPoint.X, this.Location.Y + e.Y - mPoint.Y);
            }
        }

        #endregion
        public FrmHistoryQuery()
        {
            InitializeComponent();
            this.dgv_DataList.AutoGenerateColumns = false;
            //初始化日期控件
            this.dpt_Start.Value = DateTime.Now.AddHours(-2.0);
            this.dpt_end.Value = DateTime.Now;
        }

        private void dgv_DataList_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            DataGridViewHelper.DgvRowPostPaint(this.dgv_DataList, e);
        }

        private SqlDataService sqlDataService = new SqlDataService();
        private void btn_Query_Click(object sender, EventArgs e)
        {
            TimeSpan timeSpan = this.dpt_Start.Value - this.dpt_end.Value;
            if(timeSpan.TotalSeconds < 0)
            {
                MessageBox.Show("开始时间不能大于结束时间!","查询提示");
                return;
            }
            try
            {
                DataSet dataSet = sqlDataService.getActualDataByTime(this.dpt_Start.Value, this.dpt_end.Value);
                if(dataSet.Tables.Count > 0 && dataSet.Tables[0].Rows.Count >0)
                {
                    this.dgv_DataList.DataSource = null;
                    this.dgv_DataList.DataSource = dataSet.Tables[0];
                }
                else
                {
                    MessageBox.Show("此时间段内无数据","查询提示");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("查询数据失败！" + ex.Message, "查询提示");
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
